package system

import (
	"context"

	"admin_template/bff/admin/hertz_gen/base"
	genSysUser "admin_template/bff/admin/hertz_gen/system/sys_user"
	"admin_template/bff/admin/internal/pkg/utils"
	"admin_template/bff/admin/internal/service"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

type UserRepo interface {
	service.Face
	UpdatePwd(ctx context.Context,
		c *app.RequestContext, info *genSysUser.UpdatePasswordRequest) error
	ResetPwd(ctx context.Context, userID string) error
	Auth(ctx context.Context,
		userName,
		password string) (*genSysUser.Info, error)
	OneByUUID(ctx context.Context,
		userUUID string) (*genSysUser.Info, error)
}

type UserService struct {
	*Service
}

func NewUserService(s *Service) *UserService {
	return &UserService{s}
}

// Add .
// @router /v1/system/rule [POST]
func (s *UserService) Add(ctx context.Context, c *app.RequestContext) {
	var req genSysUser.AddRequest
	s.AddFunc(s.userRepo, &req, "userRepo", "用户已存在", ctx, c)
}

// UpdateInfo .
// @router /v1/system/rule [PUT]
func (s *UserService) UpdateInfo(ctx context.Context, c *app.RequestContext) {
	var req genSysUser.UpdateRequest
	s.UpdateInfoFunc(s.userRepo, &req, "userRepo", "用户已存在", ctx, c)
}

// Del .
// @router /v1/system/rule [DELETE]
func (s *UserService) Del(ctx context.Context, c *app.RequestContext) {
	s.DelFunc(s.userRepo, "userRepo", ctx, c)
}

// UpdateState .
// @router /v1/system/rule/state [PUT]
func (s *UserService) UpdateState(ctx context.Context, c *app.RequestContext) {
	s.UpdateStateFunc(s.userRepo, "userRepo", ctx, c)
}

// List .
// @router /v1/system/rule [GET]
func (s *UserService) List(ctx context.Context, c *app.RequestContext) {
	var req genSysUser.ListRequest
	s.ListFunc(s.userRepo, &req, "userRepo", ctx, c)
}

// All .
// @router /v1/system/rule/all [GET]
func (s *UserService) All(ctx context.Context, c *app.RequestContext) {
	var req genSysUser.AllRequest
	s.AllFunc(s.userRepo, &req, "userRepo", ctx, c)
}

// UpdatePassword .
// @router /v1/system/user/password [PUT]
func (s *UserService) UpdatePassword(ctx context.Context, c *app.RequestContext) {
	var req genSysUser.UpdatePasswordRequest
	ok := s.BindAndValidate(ctx, c, &req)
	if !ok {
		return
	}
	err := s.userRepo.UpdatePwd(ctx, c, &req)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			"userRepo UpdatePwd",
			"密码错误",
			s.CtxLogger, err)
		return
	}
	reply := base.BaseReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
	}
	utils.Reply(c, consts.StatusOK, &reply)

}

// ResetPassword .
// @router /v1/system/user/reset_password [PUT]
func (s *UserService) ResetPassword(ctx context.Context, c *app.RequestContext) {
	var req genSysUser.ResetPasswordRequest
	ok := s.BindAndValidate(ctx, c, &req)
	if !ok {
		return
	}
	err := s.userRepo.ResetPwd(ctx, req.UserUuid)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			"userRepo ResetPwd",
			"",
			s.CtxLogger, err)
		return
	}
	reply := base.BaseReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
	}
	utils.Reply(c, consts.StatusOK, &reply)
}

// RuleAll .
// @router /v1/system/user/rule [GET]
func (s *UserService) RuleAll(ctx context.Context, c *app.RequestContext) {
	rolID := s.RoleID(c)
	list, err := s.ruleRepo.ListByRoleID(ctx, rolID)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			"ruleRepo ListByRoleID",
			"",
			s.CtxLogger, err)
		return
	}
	reply := genSysUser.RuleAllReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
		Data: &genSysUser.RuleAllData{List: list},
	}
	utils.Reply(c, consts.StatusOK, &reply)
}

// One .
// @router /v1/system/user/userinfo [GET]
func (s *UserService) One(ctx context.Context, c *app.RequestContext) {
	userUUID := s.UserUUID(c)
	info, err := s.userRepo.OneByUUID(ctx, userUUID)
	if err != nil {
		utils.ReplyWithErr(c,
			ctx,
			"ruleRepo ListByRoleID",
			"非法获取",
			s.CtxLogger, err)
		return
	}
	reply := genSysUser.OneReply{
		Code: base.ErrCode_Success,
		Msg:  "操作成功",
		Data: &genSysUser.OneData{UserInfo: info},
	}
	utils.Reply(c, consts.StatusOK, &reply)
}
